<template>
  <h2>当前求和为:{{ sum }}</h2>
  <button @click="sum++">点我x+1</button>
  <hr>
  <h2>姓名:{{ name }}</h2>
  <h2>年龄:{{ age }}</h2>
  <h2>展示薪水:{{ job.j1.salery }}K</h2>
  <button @click="name+='~'">修改姓名</button>
  <button @click="age++">增长年龄</button>
  <button @click="job.j1.salery++">涨薪</button>
  <button @click="showRawPerson">输出最原始的person</button>
  <h3>座驾信息:{{person.car}}</h3>
  <button @click="addcar">给人添加一台车</button>

</template>

<script>
//TODO toRaw将一个有reactive生成的响应式对象转为普通对象
//TODO markRaw标记一个对象,使其永远不在成为响应式数据
import {toRefs, reactive, ref, toRaw, markRaw} from "vue";

export default {
  name: 'Demo',
  setup() {
    //数据
    let sum = ref(0)
    let person = reactive({
      name: '张三',
      age: 18,
      job: {
        j1: {
          salery: 20
        }
      }
    })

    //方法
    function showRawPerson() {
      const p = toRaw(person)
      console.log(p)
    }

    function addcar() {
      let car = {name: '奔驰', price: '40w'}
      person.car = markRaw(car)
    }

    return {
      sum,
      //全部交出去
      person,
      //分开交出去(当有属性添加时,不会呗再次执行)
      ...toRefs(person),
      showRawPerson,
      addcar
    }
  }
}
</script>

<style>
</style>
